package xddq

import (
	"fmt"
	"time"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

// 判断当前时间与未来时间的差异，返回 time.Duration
func checkTimeDifference(currentTime, futureTime time.Time) time.Duration {
	difference := futureTime.Sub(currentTime)
	if difference > 10*time.Hour {
		return 4 * time.Hour
	} else if difference > 2*time.Hour {
		return 1 * time.Hour
	} else if difference > 20*time.Minute {
		return 10 * time.Minute
	} else if difference > 2*time.Minute {
		return 1 * time.Minute
	} else if difference > 20*time.Second {
		return 10 * time.Second
	}
	return 3 * time.Second
}

//处理仙官送福礼物信息
func (xd *XdDriver) CheckXianYuOfferingSacrificesInfoData(info D.XianYuOfferingSacrificesInfo) {
	task := xd.GetTaskByID(proto.S_YUE_BAO_ENTER)
	activityID := xd.UserData.Activity[D.Activity_Attracting_Wealth_And_Prosperity].ID
	if info.NeedOffer {
		log.Infof("福利-呱仙阁开始供奉仙玉")
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YUE_BAO_DEPOSIT, fmt.Sprintf("08 %s 10 01 18 B8 17", activityID)))
		time.Sleep(5 * time.Second)
		task.LastRun = time.Now().Add(-23 * time.Hour)
	} else {
		if time.Now().After(info.Time) {
			log.Infof("福利-呱仙阁取出[%s]供奉的仙玉", info.Time.Format("2006-01-02 15:04:05"))
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YUE_BAO_INTERACTE, "08 "+activityID))
			time.Sleep(3 * time.Second)
			log.Infof("福利-开始呱仙阁供奉仙玉")
			xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_YUE_BAO_DEPOSIT, fmt.Sprintf("08 %s 10 01 18 B8 17", activityID)))
			task.Interval = 4 * time.Hour
		} else {
			log.Infof("福利-呱仙阁需等待[%s]取出供奉的仙玉", info.Time.Format("2006-01-02 15:04:05"))
			task.StartTime = info.Time.Add(time.Second).Format("15:04:05")
			task.EndTime = info.Time.Add(time.Minute).Format("15:04:05")
			task.Interval = checkTimeDifference(time.Now(), info.Time)
		}
	}
}
